CmdUtils.CreateCommand({
  name: "revu",
  takes: {"name": noun_arb_text},
  author: { name: "Olivier Girardot", email: "ssaboum@gmail.com"},
  contributors: ["Olivier Girardot","Jonathan Winandy"],
  license: "GPL",
  description: "Look after a package on revu. v2.2",
  help: "Insert the name of the package you want to look after, it will preview the last comment added on revu (after logging in), and if you press return, it will take you to the details page of the package.",

  preview: function( pblock , person) {

    var msg = "Search for a package :";
    var baseUrl = "http://revu.ubuntuwire.com/details.py";
    var params = {package: trim(person.text)};
    pblock.innerHTML =msg;
    jQuery.get(baseUrl,
                 params, 
                 function(data) {
                  var reg = /Could not find any package with this name or upid/;
                  if(reg.test(data.valueOf())){
                        pblock.innerHTML = "No package of this name was found on REVU";
                  } else {
		        var dataPro="<i>Last comment added on this package :</i><br/>";
		        var compteurFirst = data.lastIndexOf("<table class=\"comments\"")-1;
      		        var compteurLast = data.lastIndexOf("</table>");
                        for(var i=compteurFirst;i<compteurLast;i++){
		        dataPro+=data[i];
		        }
		        pblock.innerHTML = dataPro;
		  }                   
               });
  },

  execute: function( person ) {

    var baseUrl = "http://annuaire.campus.ec-nantes.fr/resultat.php";
    var params = {nom: person.text, promo: -1,groupe: -1,option_pro: -1};
    
    var url = "http://annuaire.campus.ec-nantes.fr/fiche.php?id={QUERY}"
    var reg = /fiche.php\?id=(\d*)\"/;
    jQuery.post(baseUrl,
                 params, 
                 function(data) {
                      var query = reg.exec(data.valueOf())[1];
                      //displayMessage(query);
                      var urlString = url.replace("{QUERY}",query);
                      Utils.openUrlInBrowser(urlString);
  
                 });
  }
})


